package com.gzedu.common.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.gzedu.common.util.PropertyUtil;

public class DBHelper {
	public static final String url = PropertyUtil.readProperty("url");
    public static final String name = PropertyUtil.readProperty("driver");
    public static final String user = PropertyUtil.readProperty("username");
    public static final String password = PropertyUtil.readProperty("password");  
  
    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public DBHelper(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url, user, password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    
    public DBHelper(String sql, int[] params){
    	try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url, user, password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
            for(int i = 0; i < params.length; i++){
            	pst.setInt(i+1, params[i]);
            }
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
    
    public PreparedStatement  DBHelper(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url, user, password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return pst;
    }  
  
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}
